﻿using System.Collections.Generic;
using System.Linq;
using Comm.DAL;
using Comm.Model;
using System.Text.RegularExpressions;

namespace Comm.BLL 
{
	public partial class Articel_WordsService
	{
   		     
		/// <summary>
        /// 数据库操作对象
        /// </summary>
        private Articel_WordsDAO _dao = new Articel_WordsDAO();

        #region 向数据库中添加一条记录 +int Insert(Articel_Words model)
        /// <summary>
        /// 向数据库中添加一条记录
        /// </summary>
        /// <param name="model">要添加的实体</param>
        /// <returns>插入数据的ID</returns>
        public int Insert(Articel_Words model)
        {
            return _dao.Insert(model);
        }
        #endregion

        #region 删除一条记录 +int Delete(int id)
        public int Delete(int id)
        {
            return _dao.Delete(id);
        }
        #endregion

        #region 根据主键ID更新一条记录 +int Update(Articel_Words model)
        /// <summary>
        /// 根据主键ID更新一条记录
        /// </summary>
        /// <param name="model">更新后的实体</param>
        /// <returns>执行结果受影响行数</returns>
        public int Update(Articel_Words model)
        {
            return _dao.Update(model);
        }
        #endregion

        #region 分页查询一个集合 +IEnumerable<Articel_Words> QueryList(int index, int size, object wheres, string orderField, bool isDesc = true)
        /// <summary>
        /// 分页查询一个集合
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页大小</param>
        /// <param name="wheres">条件匿名类</param>
        /// <param name="orderField">排序字段</param>
        /// <param name="isDesc">是否降序排序</param>
        /// <returns>实体集合</returns>
        public IEnumerable<Articel_Words> QueryList(int index, int size, object wheres, string orderField, bool isDesc = true)
        {
            return _dao.QueryList(index, size, wheres, orderField, isDesc);
        }
        #endregion

        #region 查询单个模型实体 +Articel_Words QuerySingle(object wheres)
        /// <summary>
        /// 查询单个模型实体
        /// </summary>
        /// <param name="wheres">条件匿名类</param>
        /// <returns>实体</returns>
        public Articel_Words QuerySingle(object wheres)
        {
            return _dao.QuerySingle(wheres);
        }
        #endregion
        
        #region 查询单个模型实体 +Articel_Words QuerySingle(int id)
        /// <summary>
        /// 查询单个模型实体
        /// </summary>
        /// <param name="id">key</param>
        /// <returns>实体</returns>
        public Articel_Words QuerySingle(int id)
        {
            return _dao.QuerySingle(id);
        }
        #endregion

        #region 查询条数 +int QueryCount(object wheres)
        /// <summary>
        /// 查询条数
        /// </summary>
        /// <param name="wheres">查询条件</param>
        /// <returns>实体</returns>
        public int QueryCount(object wheres)
        {
            return _dao.QueryCount(wheres);
        }
        #endregion
        /// <summary>
        /// 过滤禁用词
        /// </summary>
        /// <param name="msg">用户输入的评论内容</param>
        /// <returns></returns>
        public bool CheckAllWord(string msg)
        {
            List<string> list = _dao.GetAllWord();//将找到的禁用词放入缓存中。
            //HttpRuntime.Cache["code"] = list;
            //使用正则表达式来过滤禁用词。
            string str = string.Join("|", list.ToArray());//aa|bb|cc|
            if (str.IndexOf(msg) >= 0)
            {
                return true;//进行匹配。
            }
            else 
            {
                return false;
            }
            
            //foreach (string str in list)
            //{
            //    msg.Contains(str);
            //}
        }

        /// <summary>
        /// 过滤禁用词
        /// </summary>
        /// <param name="msg">用户输入的评论内容</param>
        /// <returns></returns>
        public bool CheckAllForbid(string msg)
        {
            List<string> list = _dao.GetAllForbid();//将找到的禁用词放入缓存中。
            //HttpRuntime.Cache["code"] = list;
            //使用正则表达式来过滤禁用词。
            string str = string.Join("|", list.ToArray());//aa|bb|cc|
            return Regex.IsMatch(msg, str);//进行匹配。
            //foreach (string str in list)
            //{
            //    msg.Contains(str);
            //}
        }
        public bool CheckAllMod(string msg)
        {
            //缓存
            List<string> list = _dao.GetAllMod();//获取所有的审查词.
            string str = string.Join("|", list.ToArray());//aa|bb|cc|
            //要考虑到包含HTML代码的情况。（自己补充）
            str = str.Replace(@"\", @"\\").Replace(@"{2}", @".{0,2}");
            return Regex.IsMatch(msg, str);//进行匹配。

        }

        /// <summary>
        /// 替换词
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public string CheckReplaceWord(string msg)
        {
            List<Model.Articel_Words> list = _dao.GetReplaceWord();//放入缓存.
            string result = msg;
            foreach (Model.Articel_Words model in list)
            {
                result = result.Replace(model.WordPattern, model.ReplaceWord);
            }
            return result;
        }
	}
}